package cn.boat.router.broker.partition.strategy.impl;

import cn.boat.router.broker.partition.strategy.AbstractPartitionSelector;
import cn.boat.router.utils.PositiveAtomicCounter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 轮询分区选择器（默认）
 */
public class PollingPartitionSelector extends AbstractPartitionSelector{
    private PositiveAtomicCounter counter = new PositiveAtomicCounter();
    @Override
    public List<Integer> getPartitionDistribution0(List<Integer> totalList, Integer num) {
        List<Integer> result = new LinkedList<>();
        if (totalList.size() == 0){
            return result;
        }
        while (num-- > 0){
            result.add(totalList.get(counter.intValue()%totalList.size()));
            counter.incrementAndGet();
        }
        return result;
    }

    public static void main(String[] args) {
        PollingPartitionSelector s = new PollingPartitionSelector();
        List<Integer> list = new ArrayList<>();
//        list.add(1);
//        list.add(2);
//        list.add(3);
//        list.add(4);
//        list.add(5);
        System.out.println(s.getPartitionDistribution0(list, 3));
    }
}
